<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<!--驳回原因-->
<div class="modal fade" id="modal-create">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">驳回</h4>
            </div>
            <div class="modal-body">
                <form id="rejectForm" class="form-horizontal" role="form">
                    <input id="id" name="id" type="hidden">
                    <input id="createCode" type="hidden" name="createCode">
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">驳回原因</label>
                        <div class="col-sm-10">
                            <textarea rows="3" class="form-control" name="rejectReason"
                                      placeholder="请输入驳回原因"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-reject">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<section class="content-header">
    <h1>下料工时确认单审核</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label for="query-proCode">项目编号</label>
                                    <input id="query-proCode" type="text" name="proCode" class="form-control"
                                           placeholder="请输入项目编号">
                                </div>
                                <div class="form-group">
                                    <label for="query-proName">项目名称</label>
                                    <input id="query-proName" type="text" name="proName" class="form-control"
                                           placeholder="请输入项目名称">
                                </div>
                                <div class="form-group">
                                    <label for="query-status">状态</label>
                                    <select id="query-status" class="form-control" name="status">
                                        <option value="">请选择</option>
                                        <option value="2" selected>未审核</option>
                                        <option value="3">已审核</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="query-startTime">提报日期</label>
                                    <input id="query-startTime" type="text" class="form-control datepicker"
                                           name="startTime"
                                           readonly="readonly"/>
                                    <label for="query-endTime"> 至 </label>
                                    <input id="query-endTime" type="text" class="form-control datepicker" name="endTime"
                                           readonly="readonly"/>
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search"
                                        onclick="js.table.search(table)">
                                    <span class="Bold">搜索</span>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table" style="width: 99.9%;"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    var table;

    /** 审核数据 */
    function check(id) {
        js.modal.confirm("确认审核吗？", function () {
            js.post({
                url: ctx + "cost/dsnCost/check",
                data: {id: id},
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            })
        })
    }
    /** 撤销数据 */
    function commit(id) {
        js.modal.confirm("确认撤销吗？", function () {
            js.post({
                url: ctx + "cost/dsnCost/cancel",
                data: {id: id},
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning("该项目部分的设计工时确认单已被商务系统接收，修改前请联系商务系统相关人员撤销接收！");
                    }
                }
            })
        })
    }

    /** 打开驳回原因模态框 */
    function openRejectReason(result) {
        $("#rejectForm").fillData(result);
        js.modal.open("modal-create");
        setTimeout(function () {
            js.validate.validing("rejectForm");
        }, 500);
    }

    $(function () {

        /** 关闭驳回模态框 */
        js.modal.hideEvent("modal-create", function () {
            $("#id").val("");
            $("#rejectReason").val("");
        });

        /** 驳回 */
        $("#btn-reject").click(function () {
            let res = js.vd({
                id: 'rejectForm',
                notEmpty: ['rejectReason']
            })
            if (!res) {
                return;
            }
            js.validSubmit({
                formId: "rejectForm",
                url: ctx + "cost/dsnCost/reject",
                data: new FormData($("#rejectForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.modal.hide("modal-create");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        })

        /** 技术说明列表 */
        table = js.table.init({
            url: ctx + "craft/cost/blanking/examine",
            showExport: false,
            onLoadSuccess: function (data) {
                // var dataSource = $('#bootstrap-table').bootstrapTable('getData', true);
                // mergeCells(dataSource, "mergeFlg", "operate", 1, $('#bootstrap-table')); //合并单元格
                // mergeCells(dataSource, "proId", "proCode", 1, $('#bootstrap-table')); //合并单元格
                // mergeCells(dataSource, "proId", "proName", 1, $('#bootstrap-table')); //合并单元格
                // mergeCells(dataSource, "proId", "proNum", 1, $('#bootstrap-table')); //合并单元格
            },
            columns: [
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: "项目编号", field: 'proCode'},
                {title: "项目名称", field: 'proName'},
                {title: "项目数量", field: 'proNum'},
                {title: "专业", field: 'special'},
                {title: "部分", field: 'proPart'},
                {
                    title: "提报状态", field: 'status',
                    formatter: function (value, row) {
                        if (value === 1) {
                            return '<span class="badge label-inverse">未提交</span>';
                        } else if (value === 2) {
                            return '<span class="badge label-warning">已提交</span>';
                        } else {
                            return '<span class="badge label-success">已审核</span>';
                        }
                    }
                },
                {title: "提报日期", field: 'createTime'},
                {
                    title: "操作",
                    formatter: function (value, row) {
                        let actions = [];
                        actions.push('<a class="btn btn-info btn-xs" href="' +ctx + 'craft/cost/blanking/queryDetail/' + row.id + '" > 查看</a>');
                        if (row.status === 2) {
                            if (hasP("cost:Blanking:success")){
                                actions.push('<a class="btn btn-success btn-xs"  href="javascript:void(0)" onclick="check(' + row.id + ')"> 审核</a> ');
                                actions.push('<a class="btn btn-danger btn-xs"  href="javascript:void(0)" onclick="openRejectReason(' + js.jsonStringify(row) + ')" > 驳回</a> ');
                            }
                        }
                        // else if (row.status === 3){
                        //     actions.push('<a class="btn btn-danger btn-xs"  href="javascript:void(0)" onclick="commit(' + row.id + ')"> 撤销</a> ');
                        // }
                        return actions.join('');
                    }
                },
                // {
                //     title: "提交", field: 'operate', width: '90',
                //     formatter: function (value, row) {
                //         let actions = [];
                //         // if (hasP('dsn:proCheck:check') && row.status === 2) {
                //         if (row.status === 2) {
                //             if (hasP("cost:Blanking:success")){
                //                 actions.push('<a class="btn btn-success btn-xs"  href="javascript:void(0)" onclick="check(' + row.id + ')"> 审核</a> ');
                //                 actions.push('<a class="btn btn-danger btn-xs"  href="javascript:void(0)" onclick="openRejectReason(' + js.jsonStringify(row) + ')" > 驳回</a> ');
                //             }
                //         }else if (row.status === 3){
                //             actions.push('<a class="btn btn-danger btn-xs"  href="javascript:void(0)" onclick="commit(' + row.id + ')"> 撤销</a> ');
                //         }
                //         // }
                //         return actions.join('');
                //     }
                // }
            ]
        });

    });

    /**
     * 合并单元格
     * @param data  原始数据（在服务端完成排序）
     * @param fieldName 合并属性名称
     * @param fiedArry 待合并列
     * @param colspan   合并列
     * @param target    目标表格对象
     */
    function mergeCells(data, fieldName, fiedArry, colspan, target) {
        var valueArry = new Array();//数组只记录值
        for (var i = 0; i < data.length; i++) {
            for (var prop in data[i]) {
                if (prop == fieldName) {
                    var key = data[i][prop]
                    valueArry[i] = key;
                    break;
                }
            }
        }

        var numArry = new Array();//记录值出现的次数  一会就通过循环次数组  合并单元格
        var value = valueArry[0];//初始值
        var num = 0;//记录值出现的次数
        var index1 = 0;//numArry的下标
        for (var i = 0; i <= valueArry.length; i++) {
            if (i == valueArry.length) {//解决最后一次赋值
                numArry[index1] = num;
                break;
            }
            if (value == valueArry[i]) {
                num++;//list下一个值与变量value值相同时，num+1，下标不变
            } else {
                value = valueArry[i];//如果值不相等就把下一个值   赋值给value变量
                numArry[index1] = num;//把value变量之前的值 出现的次数记录下来
                num = 1;// 新的value值出现一次
                index1++;//下标+1;
            }
        }

        var index = 0;
        for (var i = 0; i < numArry.length; i++) {
            var count = numArry[i] * 1;
            $(target).bootstrapTable('mergeCells', {
                index: index,
                field: fiedArry,
                colspan: colspan,
                rowspan: count
            });
            index += count;
        }
    }

</script>
</body>
</html>
